﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;

namespace Lilium.Web.Mvc.Html
{
    public static class HyperLinkExtensions
    {
        public static MvcHtmlString HyperLink(this HtmlHelper html, string linkText, string url)
        {
            return HyperLink(html, linkText, url, null);
        }

        public static MvcHtmlString HyperLink(this HtmlHelper html, string linkText, string url, string target)
        {
            if (html == null) throw new ArgumentNullException("html");

            var linkBuilder = new TagBuilder("a");
            
            if (!string.IsNullOrEmpty(url))
                linkBuilder.Attributes["href"] = UrlHelper.GenerateContentUrl(url, html.ViewContext.HttpContext);
            
            if (target != null)
                linkBuilder.Attributes["target"] = target;
            linkBuilder.InnerHtml = html.Encode(linkText);
            return MvcHtmlString.Create(linkBuilder.ToString());
        }
    }
}
